import com.example.HelloSpring;
import com.example.User1;
import com.example.User2;
import com.example.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void testHelloSpring() {
//        开始创建spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//        spring容器创建成功
//        从容器根据id获取bean对象
        HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
//        打印对象信息
        helloSpring.show();
    }

    @Test
    public void testUser1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans_User1.xml");
        User1 user1 = (User1) context.getBean("user1");
        System.out.println(user1);
    }

    @Test
    public void testUser2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans_User2.xml");
        User2 user2 = (User2) context.getBean("user2");
        System.out.println(user2);
    }

    @Test
    public void testUserDao() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans_login.xml");
        UserService userService = (UserService) context.getBean("userService");
        boolean flag = userService.login("admin", "123456");
        if (flag) {
            System.out.println("登陆成功");
        } else {
            System.out.println("登陆失败");
        }
    }
}
